#! /usr/local/bin/perl

##############################################################################
#
# Convert the configuration file in the source file .
#
# @param command
#     (none)  : To perform the conversion process .
#     "clean" : To clear the output files .
#
##############################################################################

use File::Basename;
use FindBin;

#---
#--- Initial setting
#---
$CURPATH = $FindBin::Bin;

$INFILE = "kernel_cfg.cfg";
$OUTFILE_CFG_C = "kernel_cfg.c";

my $infile = $CURPATH."/".$INFILE;
my $outfile_cfg_c = $CURPATH."/".$OUTFILE_CFG_C;

$NOTICE = "This file is automatically generated. Do not change.";

#---
#--- command : "clean"
#---
if ( $ARGV[0] eq "clean" ) {
	unlink($outfile_cfg_c);
	exit(0);
}

#---
#--- set generation time
#---
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$TIMESTAMP = sprintf("%04d\/%02d\/%02d %02d\:%02d:%02d", $year+1900,$mon+1,$mday,$hour,$min);
$TIMESTAMP = "Generate time : ".$TIMESTAMP;


#---
#--- read INFILE
#---
open(in_fh, "<", $infile) or die "cannot open infile";
while(<in_fh>){
	chop;
	s/#.*$//;			# Delete comment .
	s/\s{2,}/ /g;		# Replace the two or more spaces ("\s\r\t\n\f") to one space .
	s/^\s//g;			# Remove the top of the line of the space ("\s\r\t\n\f") .
	s/\s$//g;			# Remove the end of the line of the space ("\s\r\t\n\f") .
	s/\s/\t/g;			# Replace one space to one tab .

	push(@item, $_) unless /^$/;
}
close(in_fh);


#---
#--- write OUTFILE_CFG_C
#---
open(out_fh, ">", $outfile_cfg_c) or die "cannot open outfile_cfg_c";

print out_fh "/*\n";
print out_fh " *\n";
print out_fh " * $NOTICE\n";
print out_fh " * $TIMESTAMP\n";
print out_fh " *\n";
print out_fh " */\n";
print out_fh "\n";

print out_fh "#include <mptk_typedef.h>\n";
print out_fh "EXPORT const UB system_conf [] = {\n";

my $length = @item;
for( $i=0; $i<$length; $i++ ) {
	print out_fh "\t\"$item[$i]\\n\"\n" ;
}
print out_fh "};\n";

close(out_fh);

